package com.vencent.spring.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.vencent.spring.dao.SpittleRepository;

/**
 * the spittr similar TWITTER
 * according spring's official the spittle is meaning user release the newsletter.
 * the spittler is meaning user.
 * @author wbw
 *
 */
@Controller
@RequestMapping("/spittles")
public class SpittleController {
	
	
	private SpittleRepository rep ;
	
	@Autowired
	public SpittleController(SpittleRepository rep) {
		super();
		this.rep = rep;
	}

	/**
	 * if don't provide key, the key is defaultly according by value. 
	 * for example: if the value's type is List<Spittle> , then the key is spittleList;
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String spittles(Model model){
		model.addAttribute(rep.findSpittles(Long.MAX_VALUE, 10));
		return "spittles";
	}
	
	/**
	 * if the handler mapping 's path is liking param's name , so you can ellipsis the @PathVariable 's attributes.
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "show/{id}", method = RequestMethod.GET)
	public String showSpittle(@PathVariable String id, Model model){
		model.addAttribute(rep.findOne(id));
		return "spittle";
	}
	
}
